package com.iweb.newa202104.service.impl;

import com.iweb.newa202104.Mapper.QuestionMapper;
import com.iweb.newa202104.dto.QuestionInputDTO;
import com.iweb.newa202104.dto.QuestionOutputDTO;
import com.iweb.newa202104.dto.UserOutputDTO;
import com.iweb.newa202104.entity.QuestionEntity;
import com.iweb.newa202104.enums.Err;
import com.iweb.newa202104.exception.CommonException;
import com.iweb.newa202104.service.QuestionService;
import com.iweb.newa202104.util.CommonUtil;
import com.iweb.newa202104.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.TransientDataAccessResourceException;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @Author: LGZ
 * @Date: 2021/5/11 19:12
 * @Description:
 */
@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    QuestionMapper questionMapper;

    @Override
    public ResultVO createQuestion(QuestionInputDTO questionInputDTO) {
        try {
            QuestionEntity questionEntity = CommonUtil.getMapperFactory().getMapperFacade().map(questionInputDTO, QuestionEntity.class);
            questionMapper.createQuestion(questionEntity);
            throw new CommonException(Err.SUCCESS);
        } catch (DataIntegrityViolationException e) {
            throw new CommonException(400, "分类不存在");
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getQuestionByUser(HttpSession httpSession) {
        try {
            QuestionEntity questionEntity = new QuestionEntity();
            UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
            questionEntity.setUserId(userOutputDTO.getUserId());

//            QuestionEntity[] questionEntities = questionMapper.getQuestionByUser(questionEntity);
//            QuestionOutputDTO[] questionOutputDTO = new QuestionOutputDTO[questionEntities.length];
//            CommonUtil.getMapperFactory().getMapperFacade().mapAsArray(questionOutputDTO, questionEntities, QuestionOutputDTO.class);
            QuestionOutputDTO[] questionOutputDTO = questionMapper.getQuestionByUser(questionEntity);
            return CommonUtil.ajaxReturn(questionOutputDTO);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getMyQuestionCounts(HttpSession httpSession) {
        try {
            QuestionEntity questionEntity = new QuestionEntity();
            UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
            questionEntity.setUserId(userOutputDTO.getUserId());
            return CommonUtil.ajaxReturn(questionMapper.getMyQuestionCounts(questionEntity));
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getQuestionsByCategory(int categoryId) {
        try {
            QuestionEntity questionEntity = new QuestionEntity();
            questionEntity.setCategoryId(categoryId);

//            QuestionEntity[] questionEntities = questionMapper.getQuestionsByCategory(questionEntity);
//            QuestionOutputDTO[] questionOutputDTO = new QuestionOutputDTO[questionEntities.length];
//            CommonUtil.getMapperFactory().getMapperFacade().mapAsArray(questionOutputDTO, questionEntities, QuestionOutputDTO.class);
            QuestionOutputDTO[] questionOutputDTO = questionMapper.getQuestionsByCategory(questionEntity);
            return CommonUtil.ajaxReturn(questionOutputDTO);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
    }

    @Override
    public ResultVO getAllQuestion() {
        return CommonUtil.ajaxReturn(questionMapper.getAllQuestion());
    }

    @Override
    public ResultVO acceptResponse(QuestionInputDTO questionInputDTO) {
        try {
            QuestionEntity questionEntity = CommonUtil.getMapperFactory().getMapperFacade().map(questionInputDTO, QuestionEntity.class);
            questionMapper.acceptResponse(questionEntity);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        } catch (DataIntegrityViolationException e) {
            throw new CommonException(400, "已采纳另一回答");
        }
        throw new CommonException(Err.SUCCESS);
    }

    @Override
    public ResultVO deleteQuestion(HttpSession httpSession, int questionId) {
        try {

            UserOutputDTO userOutputDTO = (UserOutputDTO)httpSession.getAttribute("user");
            QuestionEntity questionEntity = new QuestionEntity();
            questionEntity.setUserId(userOutputDTO.getUserId());
            questionEntity.setQuestionId(questionId);
            questionMapper.deleteQuestion(questionEntity);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
        throw new CommonException(Err.SUCCESS);
    }

    @Override
    public ResultVO giveLike(QuestionInputDTO questionInputDTO) {
        try {
            QuestionEntity questionEntity = CommonUtil.getMapperFactory().getMapperFacade().map(questionInputDTO, QuestionEntity.class);
            questionMapper.giveLike(questionEntity);
        } catch (TransientDataAccessResourceException e) {
            throw new CommonException(Err.PRAM_ERR);
        }
        throw new CommonException(Err.SUCCESS);
    }
}
